package com.dmzc.stage1.chapter5;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import static java.lang.Thread.currentThread;
import static java.util.concurrent.ThreadLocalRandom.current;

public class BooleanLockTest {
    private final Lock lock=new BooleanLock();
    public void syncMethod(){
        try {
            lock.lock();
            int randomInt=current().nextInt(10);
            System.out.println(currentThread()+"get the lock.");
            TimeUnit.SECONDS.sleep(randomInt);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        BooleanLockTest blt=new BooleanLockTest();
        //多个线程竞争锁
 //         IntStream.range(0,10)
//                .mapToObj(i->new Thread(blt::syncMethod))
//                .forEach(Thread::start);
        //中断线程
        new Thread(blt::syncMethod,"T1").start();
        TimeUnit.MILLISECONDS.sleep(2);
        Thread t2=new Thread(blt::syncMethod,"T2");
        t2.start();
        TimeUnit.MILLISECONDS.sleep(10);
        t2.interrupt();
        //测试阻塞的锁可超时
    }
}
